package com.lantian.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantian.common.result.Result;
import com.lantian.common.result.ResultCode;
import com.lantian.teacher.entity.EduTeacher;
import com.lantian.teacher.entity.query.TeacherQuery;
import com.lantian.teacher.exception.EduException;
import com.lantian.teacher.mapper.EduTeacherMapper;
import com.lantian.teacher.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @name:
 * @description: 讲师 服务实现类
 * @author: meet
 * @create: 2020-04-27 17:20
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Resource
    private EduTeacherMapper teacherMapper;

    @Override
    public void pageQuery(Page<EduTeacher> teacherPage, TeacherQuery query) {

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");

        if (query==null){
            baseMapper.selectPage(teacherPage, wrapper);
        }

        //获取对象属性
        String name = query.getName();
        Integer level = query.getLevel();
        Date gmtCreate = query.getGmtCreate();
        Date gmtModified = query.getGmtModified();



        //判断对象属性是否存在
        if (!StringUtils.isEmpty(name)){
            //如存在则加入查询条件
            wrapper.like("name",name);
        }

        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        //判断创建时间是否存在，如果存在，查询的是大于等于此时间的
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.ge("gmt_create", gmtCreate);
        }

        if (!StringUtils.isEmpty(gmtModified)) {
            wrapper.le("gmt_create", gmtModified);
        }

        baseMapper.selectPage(teacherPage,wrapper);
    }

   /* public Result getById(String id){

            EduTeacher teacher = teacherMapper.selectById(id);
            if (teacher==null){
                throw new EduException(ResultCode.EDU_ID_ERROR, "没有此讲师信息");
            }
            return Result.ok().data("teacher",teacher);

    }*/
}
